package com.bport.client;

import java.util.ArrayList;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class SwapPort implements EntryPoint {

	private static final int REFRESH_INTERVAL = 5000;
	private VerticalPanel mainPanel = new VerticalPanel();
	private VerticalPanel topPanel = new VerticalPanel();
	private VerticalPanel wideSectionPanel = new VerticalPanel();
	private VerticalPanel narrowSectionPanel = new VerticalPanel();
	private VerticalPanel featuredItemPanel = new VerticalPanel();
	private VerticalPanel categoryListPanel = new VerticalPanel();
	private FlexTable stocksFlexTable = new FlexTable();
	private HorizontalPanel rowOnePanel = new HorizontalPanel();
	private HorizontalPanel centerPanel = new HorizontalPanel();
	private HorizontalPanel searchPanel = new HorizontalPanel();
	private TextBox searchTextBox = new TextBox();
	private Button searchButton = new Button("Search");
	private Label lastUpdatedLabel = new Label();
	private ArrayList<String> stocks = new ArrayList<String>();
	final ListBox categoryDropBox = new ListBox(false);

	/**
	 * The constants used in this Content Widget.
	 */

	/**
	 * Entry point method.
	 */
	public void onModuleLoad() {
		// Make a command that we will execute from all leaves.
		Command cmd = new Command() {
			public void execute() {
				Window.alert("You selected a menu item!");
			}
		};

		// Make some sub-menus that we will cascade from the top menu.
		MenuBar wantMenu = new MenuBar(true);
		wantMenu.addItem("the", cmd);
		wantMenu.addItem("want", cmd);
		wantMenu.addItem("menu", cmd);

		MenuBar haveMenu = new MenuBar(true);
		haveMenu.addItem("the", cmd);
		haveMenu.addItem("have", cmd);
		haveMenu.addItem("menu", cmd);

		MenuBar myHomeMenu = new MenuBar(true);
		myHomeMenu.addItem("the", cmd);
		myHomeMenu.addItem("my home", cmd);
		myHomeMenu.addItem("menu", cmd);

		MenuBar communityMenu = new MenuBar(true);
		communityMenu.addItem("the", cmd);
		communityMenu.addItem("community", cmd);
		communityMenu.addItem("menu", cmd);

		MenuBar helpMenu = new MenuBar(true);
		helpMenu.addItem("the", cmd);
		helpMenu.addItem("help", cmd);
		helpMenu.addItem("menu", cmd);

		// Make a new menu bar, adding a few cascading menus to it.
		MenuBar menu = new MenuBar();
		menu.addItem("want", wantMenu);
		menu.addItem("have", haveMenu);
		menu.addItem("my home", myHomeMenu);
		menu.addItem("community", communityMenu);
		menu.addItem("help", helpMenu);

		categoryDropBox.addItem("bicycle");
		categoryDropBox.addItem("lawn mower");
		categoryDropBox.addItem("MP3 player");

		// Assemble search Stock.
		searchPanel.add(searchTextBox);
		searchPanel.add(categoryDropBox);
		searchPanel.add(searchButton);
		searchPanel.addStyleName("searchPanel");

		// Assemble row one panel
		rowOnePanel
				.add(new HTML("<img src=\"images/SwapPortLogoTemp.png\" />"));
		rowOnePanel.add(new HTML("<h1><a name=top>Swap Port</a></h1>"));
		rowOnePanel.add(menu);
		rowOnePanel.addStyleName("rowOnePanel");

		// Assemble top panel
		topPanel.add(rowOnePanel);
		topPanel.add(searchPanel);
		topPanel.addStyleName("topPanel");

		// Assemble featuredItemPanel
		featuredItemPanel.add(new HTML("<H4>Featured Item in Your Area</H4>"));
		featuredItemPanel.add(new HTML(
				"<img src=\"images/bicycle-yellow-small.png\"/>"));
		featuredItemPanel.addStyleName("featuredItemPanel");

		// Assemble categoryListPanel
		//categoryListPanel.add(new HTML("<a href=\"/temp.html\">books</a>"));
		categoryListPanel.add(new HTML("<a href=\"\">books</a>"));
		Hyperlink link0 = new Hyperlink("computers",
				"top");
		categoryListPanel.add(link0);
		categoryListPanel
				.add(new HTML("<a href=\"/SwapPort.html\">pianos</a>"));
		categoryListPanel
		.add(new HTML("<a href=\"/SwapPort.html\">MP3 players</a>"));
		categoryListPanel.addStyleName("categoryListPanel");

		// Assemble narrowSectionPanel
		narrowSectionPanel.add(featuredItemPanel);
		narrowSectionPanel.add(categoryListPanel);
		narrowSectionPanel.addStyleName("narrowSectionPanel");

		// Assemble wideSectionPanel
		wideSectionPanel.add(new HTML(
				"<img src=\"images/SwapPortLogoTemp.png\" />"));
		wideSectionPanel.addStyleName("wideSectionPanel");

		// Assemble centerPanel
		centerPanel.add(narrowSectionPanel);
		centerPanel.add(wideSectionPanel);
		centerPanel.addStyleName("centerPanel");

		// Assemble mainPanel.
		mainPanel.add(topPanel);
		mainPanel.add(centerPanel);
		mainPanel.addStyleName("mainPanel");

		// Add it to the root panel.
		RootPanel.get("swapPortHome").add(mainPanel);

		// Move cursor focus to the searchTextBox.
		searchTextBox.setFocus(true);

		// Listen for mouse events on the searchButton.
		searchButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				searchWantedItems();
			}
		});
		// Listen for keyboard events in the input box.
		searchTextBox.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(KeyPressEvent event) {
				if (event.getCharCode() == KeyCodes.KEY_ENTER) {
					searchWantedItems();
				}
			}
		});

	}

	/**
	 * Initialize this example.
	 */
	/*
	 * public Widget onInitialize() { // Wrap the content in a DecoratorPanel
	 * DecoratorPanel decPanel = new DecoratorPanel();
	 * decPanel.setWidget(mainPanel); return decPanel; }
	 */
	/**
	 * Search wanted items. Executed when the user clicks the searchButton or
	 * presses enter in the searchTextBox.
	 */
	private void searchWantedItems() {
		final String symbol = searchTextBox.getText().trim();
		searchTextBox.setFocus(true);
		Window.alert("Searching for wanted item: " + symbol);
		// Stock code must be between 1 and 10 chars that are numbers, letters,
		// or dots.
		if (!symbol.matches("^[0-9A-Za-z\\.]{1,10}$")) {
			Window.alert("'" + symbol + "' is not a valid symbol.");
			searchTextBox.selectAll();
			return;
		}

		searchTextBox.setText("");
	}
}
